package com.linewow.xhyy.nihaonew.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.liangfeizc.flowlayout.FlowLayout;
import com.linewow.xhyy.nihaonew.R;
import com.linewow.xhyy.nihaonew.activity.TopicDetailActivity;
import com.linewow.xhyy.nihaonew.entity.user.Uinfo;
import com.linewow.xhyy.nihaonew.entity.image.SimpleImagesInfo;
import com.linewow.xhyy.nihaonew.entity.tag.CustomTagsInfo;
import com.linewow.xhyy.nihaonew.entity.topic.FindTagTopicInfo;
import com.linewow.xhyy.nihaonew.model.HttpModel;
import com.linewow.xhyy.nihaonew.view.ColorfulTag;
import com.linewow.xhyy.nihaonew.view.VoiceButton2Red;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LXR on 2016/9/9.
 * 多小图话题  关注列表
 */
public class FindTagTopicAdapter1 extends BaseQuickAdapter<FindTagTopicInfo<Uinfo, SimpleImagesInfo, CustomTagsInfo>> {


    private LayoutInflater layoutInflater;
    private Context context;
    private Transformation transformation;


    private String TAG="FindTagTopicAdapter1";
    public FindTagTopicAdapter1(List<FindTagTopicInfo<Uinfo, SimpleImagesInfo, CustomTagsInfo>> data) {
        super(R.layout.item_tag_detail1,data);
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, final FindTagTopicInfo<Uinfo, SimpleImagesInfo, CustomTagsInfo> findTagTopicInfo) {
        if(context==null){
            context=baseViewHolder.getConvertView().getContext();
        }
        List<SimpleImagesInfo>simpleImagesInfos=findTagTopicInfo.images;
        Picasso.with(baseViewHolder.getConvertView().getContext()).load(findTagTopicInfo.avatar.replace("m.en1on1.com", "huibenabc.com")).fit().error(R.mipmap.err_avatar).into((ImageView) baseViewHolder.getView(R.id.widget_personal_message_avatar));
        baseViewHolder.setText(R.id.widget_personal_message_nickname,findTagTopicInfo.uinfo.nickname);
        if(findTagTopicInfo.location.equals("null")){
            baseViewHolder.setVisible(R.id.widget_personal_message_address_line,false);
        }else{
            baseViewHolder.setVisible(R.id.widget_personal_message_address_line,true);
            baseViewHolder.setText(R.id.widget_personal_message_address_tv,findTagTopicInfo.location);
        }
        baseViewHolder.setText(R.id.widget_personal_message_time_tv,findTagTopicInfo.time);


        baseViewHolder.setVisible(R.id.item_tag_detail_content,true);
        if(findTagTopicInfo.subject.equals("null")){
            if(findTagTopicInfo.contents.equals("")){
                baseViewHolder.setVisible(R.id.item_tag_detail_content,false);
            }else{
                baseViewHolder.setText(R.id.item_tag_detail_content,findTagTopicInfo.contents);
            }
        }else{
            baseViewHolder.setText(R.id.item_tag_detail_content,findTagTopicInfo.subject);
        }
        VoiceButton2Red voiceButton=baseViewHolder.getView(R.id.item_tag_detail_voice_button);
        if(findTagTopicInfo.audiotime.endsWith("0")){
            voiceButton.setVisibility(View.GONE);
        }else{
            voiceButton.setVisibility(View.VISIBLE);
            voiceButton.setUrlPath(findTagTopicInfo.audiotime,findTagTopicInfo.audiopath);

        }

        if(findTagTopicInfo.zan.equals("1")){
            baseViewHolder.setImageResource(R.id.widget_topic_menu_like_img,R.mipmap.icon_like_red);
        }else{
            baseViewHolder.setImageResource(R.id.widget_topic_menu_like_img,R.mipmap.icon_like_gray);
        }


        baseViewHolder.setText(R.id.widget_topic_menu_like_tv,findTagTopicInfo.upnum);
        baseViewHolder.setText(R.id.widget_topic_menu_topic_tv,findTagTopicInfo.replynum);
        baseViewHolder.addOnClickListener(R.id.widget_topic_menu_like_rela);
        baseViewHolder.addOnClickListener(R.id.widget_topic_menu_conversation_img);
        baseViewHolder.addOnClickListener(R.id.widget_personal_message_avatar);
        Picasso.with(baseViewHolder.getConvertView().getContext()).load((simpleImagesInfos.get(0).image+ HttpModel.IMAGEBIGSUFFIX).replace("m.en1on1.com", "huibenabc.com")).fit().centerCrop().error(R.mipmap.err_topic)
                .into((ImageView) baseViewHolder.getView(R.id.item_tag_detail_img1));
        RecyclerView itemTagDetailRecycler=baseViewHolder.getView(R.id.item_tag_detail_recycler1);
        LinearLayoutManager manager=new LinearLayoutManager(baseViewHolder.getConvertView().getContext());
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        manager.setStackFromEnd(true);
        itemTagDetailRecycler.setLayoutManager(manager);
        itemTagDetailRecycler.setItemAnimator(new DefaultItemAnimator());
        ImagesHorizontalAdapter adapter=new ImagesHorizontalAdapter(simpleImagesInfos,baseViewHolder.getConvertView().getContext());
        itemTagDetailRecycler.setAdapter(adapter);

        FlowLayout flowLayout=baseViewHolder.getView(R.id.item_tag_detail_flowlayout);
        flowLayout.removeAllViews();
        List<CustomTagsInfo> list=new ArrayList<CustomTagsInfo>();
        list.addAll(findTagTopicInfo.tags);
        list.addAll(findTagTopicInfo.customtags);
        if(list.size()==0){
            baseViewHolder.setVisible(R.id.item_tag_detail_icon_tag_rela,false);
        }else{
            baseViewHolder.setVisible(R.id.item_tag_detail_icon_tag_rela,true);
            setCustomTags(flowLayout,list);
        }
    }


    private void setCustomTags(FlowLayout item1TopicDetailFlowlayout,List<CustomTagsInfo>list) {
            for(CustomTagsInfo temp:list){
                Log.e(TAG,"所有的tag"+temp.zhcn);
                ColorfulTag colorfulTag = new ColorfulTag(context);
//                colorfulTag.setHei(dp2px(15));
                colorfulTag.setCustomTagsInfo(temp);
                item1TopicDetailFlowlayout.addView(colorfulTag);
                colorfulTag.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        CustomTagsInfo info = (CustomTagsInfo) view.getTag();
                        Log.e(TAG,"点击的结果"+info.tagid);

                    }
                });
            }

    }

    public int dp2px(int dpValue) {
        return (int) (dpValue * context.getResources().getDisplayMetrics().density);
    }

    public void startDetailActivity(String tid){
        Intent intent=new Intent(context, TopicDetailActivity.class);
        intent.putExtra("msg",tid);
        context.startActivity(intent);
    }
}
